package com.ruoyi.station.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.station.domain.SDistributionFrame;
import com.ruoyi.station.domain.SMachineCabinet;
import com.ruoyi.station.domain.SStation;
import com.ruoyi.station.mapper.SDistributionFrameMapper;
import com.ruoyi.station.mapper.SMachineCabinetMapper;
import com.ruoyi.station.service.SDistributionFrameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 配线架Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-06
 */
@Slf4j
@Service
public class SDistributionFrameServiceImpl extends ServiceImpl<SDistributionFrameMapper, SDistributionFrame> implements SDistributionFrameService {

    @Autowired
    private SDistributionFrameMapper sDistributionFrameMapper;
    @Autowired
    private SMachineCabinetMapper sMachineCabinetMapper;
    /**
     * 导入配线架数据
     * @param file
     * @return
     */
    @Override
    @Transactional
    public String importsDistributionFrames(MultipartFile file) {
        String username = SecurityUtils.getUsername();
        List<List<Object>> lists = FileUtils.parseUploadFile(file);
        int i = 3;
        for (List<Object> readFrame : lists) {
            //所属机柜
            String machineCabinetName = String.valueOf(readFrame.get(1)).trim();
            SMachineCabinet machineCabinet = sMachineCabinetMapper.selectOne(
                    new LambdaQueryWrapper<SMachineCabinet>().eq(SMachineCabinet::getMachineCabinetName, machineCabinetName));
            if (machineCabinet == null ) return "第" + i +"行机柜不存在！";
            //配线架类型
            String distributionFrameType = DictUtils.getDictValue("frame_type", String.valueOf(readFrame.get(2)).trim());
            if(StringUtils.isBlank(distributionFrameType)) return "第" + i +"行配线架类型不存在！";
            //端口行
            String portLine = String.valueOf(readFrame.get(3)).trim();
            if(StringUtils.isBlank(portLine)) return "第" + i +"行端口行不正确！";
            //端口列
            String portColumn = String.valueOf(readFrame.get(4)).trim();
            if(StringUtils.isBlank(portColumn)) return "第" + i +"行端口列不正确！";

            SDistributionFrame build = SDistributionFrame.builder()
                    .machineCabinet(machineCabinet.getId())
                    .distributionFrameType(distributionFrameType)
                    .portLine(Integer.parseInt(portLine))
                    .portColumn(Integer.parseInt(portColumn))
                    .updateFlag(1)
                    .createBy(username)
                    .createTime(DateUtil.date())
                    .build();
            int insert = sDistributionFrameMapper.insert(build);
            if (insert < 1) return "第" + i +"行保存出错了！";
            i++;
        }
        return "成功导入"+lists.size()+"条";
    }
}
